package com.kady.controller.admin;

import com.kady.entity.ResultVO;
import com.kady.entity.User;
import com.kady.service.AdminService;
import com.kady.service.impl.AdminServiceImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: kady
 * \* Date: 2021/3/23
 * \* Time: 17:15
 * \* 千峰JAVA2101班
 * \* Description: 登录控制
 * \
 */

@WebServlet("/userService/login")
public class AdminLoginController extends HttpServlet {

    /**
     * 登录接口,不走BaseServlet
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object iCode = req.getSession().getAttribute("i-code");
        String recCode = req.getParameter("code");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        req.getSession().setAttribute("username",username);
        req.getSession().setAttribute("password",password);
        if(iCode != null){
            if(((String)iCode).equalsIgnoreCase(recCode)){
                AdminService adminService = new AdminServiceImpl();
                try {

                    Object obj = adminService.login(username,password);
                    if(obj instanceof User){
                        User user = (User) obj;
                        if(user!=null){
                            /**
                             * 判断是否查询到用户
                             */
                            if((int)user.getIs_delete()==1){
                                /**
                                 * 判断用户是否为已删除状态
                                 */
                                req.getSession().setAttribute("info","用户不存在");
                                resp.sendRedirect(req.getContextPath()+"/login.jsp");
                                return;
                            }
                            req.getSession().removeAttribute("info");
                            if((int)user.getIs_admin()==1){
                                /**
                                 * 判断是否为管理员来登录后台和前台
                                 */
                                req.getSession().setAttribute("adminUser",user);
                                resp.sendRedirect(req.getContextPath() + "/backend/index.jsp");
                            }else{
                                req.getSession().setAttribute("frontUser",user);
                                resp.sendRedirect(req.getContextPath() + "/front/index.jsp");
                            }
                            return;
                        }else{
                            req.getSession().setAttribute("info","找不到用户");
                            resp.sendRedirect(req.getContextPath()+"/login.jsp");
                        }
                        return;
                    }else{
                        req.getSession().setAttribute("info",((ResultVO)obj).getMessage());
                        resp.sendRedirect(req.getContextPath()+"/login.jsp");
                    }
                    return;
                } catch (SQLException throwables) {
                    resp.sendRedirect(req.getContextPath()+"/login.jsp");
                    throwables.printStackTrace();
                }
            }else{
                req.getSession().setAttribute("info","验证码错误！");
                resp.sendRedirect(req.getContextPath()+"/login.jsp");
            }
        }
    }
}
